import com.strutor.stack.ArrayStack;
import com.strutor.stack.BaseStack;
import com.strutor.stack.LinkedStack;
import sun.misc.Unsafe;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Stack;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

public class Main {

    public static void main(String[] args)throws Exception {
        AtomicInteger integer = new AtomicInteger(0);
        int Cast = 100;

        Object lock = new Object();

         CountDownLatch countDownLatch = new CountDownLatch(Cast);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < Cast; i++) {
            executorService.execute(()->{
                try {
                    System.out.println("xxxxxxxxxxxx");
                    LockSupport.park(100);//纳秒
                    System.out.println("xxx");

                    System.out.println("..........");
                    integer.addAndGet(1);
                    integer.compareAndSet(0,1);
                    countDownLatch.countDown();
                    synchronized (lock){
                        lock.wait();
                        System.out.println("********");
                    }
                    System.out.println("????????????????");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        LockSupport.unpark(Thread.currentThread());


        countDownLatch.await();
        List list = new ArrayList<>();

        System.out.println(integer.get());

        synchronized (lock){
            lock.notify();
        }
    }

    private static double testStack(BaseStack<Integer> stack, int opCount){
        long startTime = System.currentTimeMillis();

        Random random = new Random();
        for (int i = 0; i < opCount; i++) {
            stack.push(random.nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0; i < opCount; i++) {
            stack.pop();
        }
        Stack stack1 = new Stack();
        long endTime = System.currentTimeMillis();
        return (endTime - startTime)/1000.0;
    }
}
